package models

import (
	_ "github.com/Go-SQL-Driver/MySQL"
	"github.com/astaxie/beego/orm"
	"time"
)

//表的设计
type User struct {
	Id       int
	UserName string
	Passwd   string
	Articles []*Article `orm:"rel(m2m)"` //多对多
}

//文章表和文章类型表是一对多
type Article struct {
	Id2         int          `orm:"pk;auto"`
	Title       string       `orm:"size(20)"`  //文章标题
	Content     string       `orm:"size(500)"` //文章内容
	Img         string       `orm:"size(50);null"`
	Time        time.Time    `orm:"type(datatime);auto_now_add"` //发布时间
	Count       int          `orm:"default(0)"`                  //阅读量
	ArticleType *ArticleType `orm:"rel(fk)"`
	Users       []*User      `orm:"reverse(many)"` //多对多
}

type ArticleType struct {
	Id       int
	TypeName string     `orm:"size(20)"`
	Article  []*Article `orm:"reverse(many)"`
}

func init() {
	//注册数据库
	orm.RegisterDataBase("default", "mysql", "root:12345678@tcp(127.0.0.1:3306)/class1?charset=utf8")
	//注册表
	orm.RegisterModel(new(User), new(Article), new(ArticleType))
	orm.RunSyncdb("default", false, true)

}
